package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Byqpdsensor;
import com.example.onlinemonitoring.model.Dlhlsensor;
import com.example.onlinemonitoring.serviceImpl.ByqpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlhlsensorServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("dlhl61850Com")
@Scope("prototype")
public class Dlhl61850Com extends CommPortManager {
    @Autowired
    private DlhlsensorServiceImpl dlhlsensorserviceimpl;
    HashMap<Integer, String> devmap = new HashMap<>();

    @Override
    @Transactional
    public void receiveData(String str) {
        setText("收到sanway请求：" + str + "\r\n");
        if (str.length() == 16) {
            devmap.clear();
            int startAddr = parseHex4(str.substring(4, 8));
            int addrLength = parseHex4(str.substring(8, 12));
            Dlhlsensor dlhl = new Dlhlsensor();
            dlhl.setStationid(ad.getStationid());
            dlhl.setDevicegroup(ad.getDevicegroup());
            List<Dlhlsensor> list = dlhlsensorserviceimpl.getLists(dlhl);
            int index = 0;
            for (int i = 0; i < list.size(); i++) {
                dlhl = list.get(i);
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastacurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastbcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastccurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastdcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLasta1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastb1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastc1current()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastd1current()) * 100)));
            }
            /*--------------- 这一段自定义代码 --------------- */
            String head = str.substring(0, 4);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
            s += CRC16.calcCrc16(s);
            Send(s);
        }
    }
}
